<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Model\Ad;
use App\Http\Model\Slide;
use Illuminate\Support\Facades\Input;
use DB;


class AdController extends Controller
{   
      //修改排序
    public function changeOrder()
    {   
       
       $input = Input::except('_token');
       // dd($input);

    
       $input['id'];
       $input['order'];
//       通过id找到要修改记录
       $ad = Ad::find($input['id']);
       $ad->order = $input['order'];
       $res = $ad->save();

       $data = [];
       if($res){
            $data['status'] = 0;
            $data['msg']  = '修改成功';
       }else{
           $data['status'] = 1;
           $data['msg']  = '修改失败';
       }
       return $data;
    }
      //    文件上传
    public function upload(Request $request)
    {

//        获取文件对象
        $file = Input::file('file_upload');
       
//        判断文件是否有效
        if($file->isValid()){
            $entension = $file->getClientOriginalExtension();//上传文件的后缀名
            $newName = date('YmdHis').mt_rand(1000,9999).'.'.$entension;

//            1上传到本地服务器
            $path = $file->move(public_path().'/ad_uploads',$newName);
//            2上传到七牛云
//            writeStream（要上传的七牛存储空间的路径，你要上传的文件当前服务器上的路径）
//            \Storage::disk('qiniu')->writeStream('uploads/'.$newName, fopen($file->getRealPath(), 'r'));


            //3阿里 OSS 图片上传
            // 获取图片在临时文件中的地址
//            $pic = $file->getRealPath();
//            $result = OSS::upload('uploads/'.$newName, $pic);

            $filepath = 'ad_uploads/'.$newName;
            return  $filepath;
        }
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        // 加载广告管理页面
        $count = $request -> input('count',5);
        $request = $request ->all();
        // 查询数据并分页
        $data = Ad::orderBy('order')->paginate($count);
        // 加载模版
        return view('admin.ad.index',['data'=>$data,'request'=>$request]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
         // 加载 添加广告信息 页面
        return view('admin.ad.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
         //获取到用户提交过来的数据
        $data = $request ->except('_token');
       
        
        // 处理插入
        $ad = new Ad;
        $ad->pic = $data['pic'];
        $ad->url = $data['url'];
        $ad->order = $data['order'];
        $ad->description = $data['description'];
        
        // 判断 跳转页面

        if($ad->save()){
            return redirect('/admin/ad');
        }else{
            return back()->with('errors','添加失败');
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
       $data = [];
       $aa = Ad::find($id);
       $res1 = $aa->delete();
       $res2 = \Storage::disk('public')->delete($aa->pic);
      
        //  删除成功
       if($res1 && $res2){
            $data['status']= 0;
            $data['msg']='删除成功';
       }else{
           $data['status']= 1;
           $data['msg']='删除失败';
       }
       return $data;
    }
}
